<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/10
 * Time: 16:57
 */
class ZL_Model extends CI_Model{
    //要操作的表名字
    public $table = '';
    public $table_key = '';
    //order_by 的列
    public $table_order_by = '';
    //order_by 的ASC还是DESC;
    public $table_order_sort = '';
    //limit操作
    public $limit = '';

    //调用父类方法 类似OC中的 super.viewDidLoad
    function __construct(){
        parent::__construct();
        $this->load->database();
    }

    /**
     * 获取一个表中的数据 where,order by、limit
     *
     * @param array $where 条件 列=>值
     * @return mixed
     */
    public function get_all($where = '',$limit_satrt = '',$limit_end = '',$selectField ='*'){
        if (is_array($where)){
            $this->db->where($where);
        }
        $this->db->select($selectField);
        $this->db->order_by($this->table_order_by,$this->table_order_sort);
        $this->db->limit($limit_end,$limit_satrt);
        return $this->db->get($this->table);
    }

    /**
     * 从数据库中查询一条数据
     *
     * @param $this->table string 需要获取的表名
     * @param $where array 条件 列=>值
     * @param $selectField string 需要选择的列名 format为 '*,*,*'
     *
     */
    public function search($where,$selectField = '*'){
        if (is_array($where)){
            foreach ($where as $k => $v){
                if ($v === false || $v === ''){
                    $this->db->where($k);
                }else{
                    $this->db->where($k,$v);
                }
            }
            $this->db->select($selectField);
            $this->db->order_by($this->table_order_by,$this->table_order_sort);
            $this->db->from($this->table);
            $this->db->limit(1);
            $query = $this->db->get();
            return $query->row();
        }else{
            return false;
        }
    }

    /**
     * 插入数据到数据库
     *
     * @param $table string 表名
     * @param $data array 要插入的数据 列=>值
     *
     */
    public function insert($data){
        if (is_array($data)){
            return $this->db->insert($this->table, $data);
        }else{
            return false;
        }
    }
    /**
     * 自定义查询语句
     *
     * @param $query string sql语句
     *
     */

    public function sql_query($query){
        $query = $this->db->query($query);
        return $query;
    }

    /**
     * @param $set array 需要更新的 列=>值
     * @param $where string 更新条件
     */
    public function update($set,$where){
        if (is_array($set)){
            $this->db->where($this->table_key, $where);
            return $this->db->update($this->table, $set);
        }else{
            return false;
        }
    }

    /**
     * @param $where array 删除条件
     */

    public function delete($where){
        if (is_array($where)){
            return $this->db->delete($this->table,$where);
        }else{
            return false;
        }
    }

    /**
     * @param array $where 条件 列=>值
     */
    public function count_all($where = ''){
        if (is_array($where)){
            $this->db->where($where);
        }
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    /**
     * @param string $where
     * @param string $limit_satrt
     * @param string $limit_end
     * @return mixed
     */
    public function like($where='',$limit_satrt = '',$limit_end = ''){
        if (is_array($where)){
            $this->db->like($where);
        }
        $this->db->order_by($this->table_order_by,$this->table_order_sort);
        $this->db->limit($limit_end,$limit_satrt);
        return $this->db->get($this->table);
    }

    /**
     * @param $where array where in 语法 where $this->table_key in array('item1','item2',...)
     */
    public function where_in($where,$limit_satrt='',$limit_end=''){
        if (is_array($where)){
            $this->db->where_in($this->table_key,$where);
        }
        $this->db->order_by($this->table_order_by,$this->table_order_sort);
        $this->db->limit($limit_end,$limit_satrt);
        return $this->db->get($this->table);
    }

    /**
     * @param $field string 需要统计的列名
     * @return mixed
     */
    public function sum($field){
        $this->db->select_sum($field);
        $query = $this->db->get($this->table);
        $arr = $query->row();
        return $arr->$field;
    }
}